/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function InitGoalLocations ()
	Msg("Loaded Goal Locations\n") FormattedStatus = FormattedStatus ..  "Loaded Goal Locations\n"
	
	tabzpm = ents.FindByName("ent_zpm")
	CurMap = game.GetMap()
	ReadCurMap = file.Read("StargateWW/Goals/Maps.txt")
	tempReadCurMap = (string.Explode( " " ,ReadCurMap))
	
	if not ReadCurMap then
		file.Write("StargateWW/Goals/Maps.txt","")
	end
	
	for k,v in pairs(tempReadCurMap) do
		if v == CurMap then
			Msg("This map is a Goal map, continue to load!\n") FormattedStatus = FormattedStatus ..  "This map is a Goal map, continue to load!\n"
			FoundIt = true
		end
	end
	
	if !FoundIt == true then
		Msg("WARNING:  This is not a goal map!  Loading Score System!\n") FormattedStatus = FormattedStatus ..  "WARNING:  This is not a goal map!  Loading Score System!\n"
		hook.Remove("Think", "CheckZPM")
		return
	end
	
	for k,v in pairs(tabzpm) do
			tzpmPos = v:GetPos()
	end
	
	file.Write("StargateWW/Goals/" .. game.GetMap() .. "_zpm.txt",tostring(tzpmPos))
	
	FILEtempzpmPos = file.Read("StargateWW/Goals/" .. game.GetMap() .. "_zpm.txt")
	tempzpmPos = (string.Explode(" " ,FILEtempzpmPos))
	zpmPos = Vector(tonumber(tempzpmPos[1]),tonumber(tempzpmPos[2]),tonumber(tempzpmPos[3]))
	NotThere = 1
	ATLANTISCAPTURED = 0
	WRAITHCAPTURED = 0
end
	
function CheckZPM ()
	
	if (table.concat(tempzpmPos, " ") == "nil") then
		InitGoalLocations()
	end
	
	local ent = ents.FindInSphere(zpmPos, 200)
	
	if NotThere == nil then
		NotThere = 1
	end
	
	for k,v in pairs(ent) do
		if v:IsPlayer() and NotThere == 1 then
			NotThere = 0
			Notify(v, "You have 20 seconds to defend and capture the ZPM!", 5)
			v:SetNWInt("ZPMTeam", v:Team())
			timer.Simple(10, function() CapturedZPM(v) end)
		end
	end
end

hook.Add("Think", "CheckZPM", CheckZPM)

function CapturedZPM(v)
	for k,v in pairs(tabzpm) do
		v:Fire("use","",0)
	end
	
	local CaptureTeam = v:GetNWInt("ZPMTeam")
	if CaptureTeam >= 10 and CaptureTeam < 19 then
		NotifyAll("Atlantis has captured the ZPM!", 5)
		v:SetNWInt("ZPMCAPTURED", 1)
	end
	
	if CaptureTeam >= 20 and CaptureTeam < 29 then
		NotifyAll("The Wraith have captured the ZPM!", 5)
		v:SetNWInt("ZPMCAPTURED", 1)
	end
end

function lol(p)
	local trace = p:GetEyeTrace()
	local hitpos = trace.HitPos
	
	timer.Simple(1, function()
	Notify(p, trace.Entity:GetName(), 5)
	end)
end
concommand.Add("Shit", lol)